package com.hunan.api.catv.mq.consumer;


import com.google.gson.JsonObject;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.im.ImBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class MemberConfConsumer {

    public static Logger log = LoggerFactory.getLogger(MemberConfConsumer.class);

    @Autowired
    ImBiz imBiz;

    @Autowired
    MemberBiz memberBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.member_conf_queue, durable = "true"), exchange = @Exchange(value = MqConstants.member_conf_exchange, durable = "true", type = "topic"), key = MqConstants.member_conf_routingkey))
    public void memberConf(String msg) {
        JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
        String type = msgJson.get("type").getAsString();
        Integer memberId = msgJson.get("memberId").getAsInt();
        String pushToken = null;
        try {
            if ("im".equals(type)) {
                imBiz.importAccount(memberId);
            }
            if ("push".equals(type)) {
                pushToken = msgJson.get("pushToken").getAsString();
            }
            memberBiz.edit(memberId, pushToken);
        } catch (Exception e) {
            log.error("[MemberConf-Error: [{}]-[{}]]", msg, e);
        }

    }
}
